package com.blog.service.impl;

import com.blog.dao.ArticlesDao;
import com.blog.dao.UserDao;
import com.blog.dao.impl.ArticlesDaoImpl;
import com.blog.dao.impl.UserDaoImpl;
import com.blog.dto.FavoriteDTO;
import com.blog.pojo.Articles;
import com.blog.pojo.Favorite;
import com.blog.pojo.Start;
import com.blog.pojo.User;
import com.blog.service.ArticleService;
import com.blog.websocket.NotificationWebSocket;

import java.util.List;

public class ArticleServiceImpl implements ArticleService {
    private ArticlesDao articlesDao=new ArticlesDaoImpl();
    private UserDao userDao=new UserDaoImpl();


    @Override
    public Articles addArticle(Articles articles) {
         articlesDao.addArticles(articles);
         return articles;

    }

    @Override
    public boolean delete(int id) {
       int update= articlesDao.deleteArticles(id);
        if(update>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Articles articles) {
        int update= articlesDao.updateArticles(articles);
        if(update>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Articles> findByAuthor(String author) {
      return   articlesDao.findByAuthor(author);
    }


    @Override
    public Articles findByTitle(String title) {
      return   articlesDao.findByTitle(title);
    }

    @Override
    public List<Articles> findAll() {
        return articlesDao.findAll();
    }

    @Override
    public Articles findById(int id) {
        return articlesDao.findById( id);
    }

    @Override
    public List<Articles> finbByUserId(int userId) {
        return articlesDao.finbByUserId(userId);
    }

    @Override
    public List<Articles> findByUserIdAndStatus(int userId, byte status) {
        return articlesDao.findByUserIdAndStatus(userId, status);
    }

    @Override
    public boolean deleteStart(int articleId, int userId) {
        int delete = articlesDao.deleteStart(articleId, userId);
        if(delete>0){
            return true;
        }
        return false;
    }

    @Override
    public Start addStart(Start start) {
       articlesDao.addStart(start);
//       sendStarNotify(start.getUserName(), start.getTitle(), start.getUserId().toString());
        User starUser = userDao.findUserById(start.getUserId());
        Articles article = articlesDao.findById(start.getArticleId());
        sendStarNotify(starUser.getUsername(),article.getTitle(),article.getUserId().toString());
        return start;
    }
    public void sendStarNotify(String startUser, String title, String articleAuthorId) {
        NotificationWebSocket.NotificationMessage message =
            new NotificationWebSocket.NotificationMessage(
                "comment",
                "点赞提醒",
                startUser + " 点赞了您的文章",
                title,
                articleAuthorId
            );

        // 发送通知给文章作者
        System.out.println(String.format("发送通知给文章作者, 评论人:%s, 文章标题:%s,作者ID:%s", startUser, title, articleAuthorId));
        NotificationWebSocket.sendNotification(articleAuthorId, message);
    }
    @Override
    public long findStartCount(int articleId) {

        return articlesDao.findStartCount(articleId, (byte) 1);

    }

    @Override
    public Favorite addFavorite(Favorite favorite) {
        articlesDao.addFavorite(favorite);
        User favoriteUser = userDao.findUserById(favorite.getUserId());
        Articles article = articlesDao.findById(favorite.getArticleId());
        sendFavoriteNotify(favoriteUser.getUsername(),article.getTitle(),article.getUserId().toString());

        return favorite;
    }

    public void sendFavoriteNotify(String favoriteUser, String title, String articleAuthorId) {
        NotificationWebSocket.NotificationMessage message =
            new NotificationWebSocket.NotificationMessage(
                "comment",
                "收藏提醒",
                favoriteUser + " 收藏了您的文章"+","+title,
                title,
                articleAuthorId
            );

        // 发送通知给文章作者
        System.out.println(String.format("发送通知给文章作者, 评论人:%s, 文章标题:%s,作者ID:%s", articleAuthorId, title, articleAuthorId));
        NotificationWebSocket.sendNotification(articleAuthorId, message);
    }



    @Override
    public boolean deleteFavorite(int articleId, int userId) {
        int delete = articlesDao.deleteFavorite(articleId, userId);
        if(delete>0){
            return true;
        }
        return false;
    }

    @Override
    public long findFavoriteCount(int articleId) {
        return articlesDao.findFavoriteCount(articleId);
    }

    @Override
    public boolean findStart(int articleId, int userId, Byte start) {
        return articlesDao.findStart(articleId, userId, start);
    }

    @Override
    public boolean findFavorite(int userId, int articleId, Byte favorite) {
        return articlesDao.findFavorite(userId, articleId, favorite);
    }

    @Override
    public boolean updateStartCount(int articleId, long startCount) {
        int update= articlesDao.updateStartCount(articleId, startCount);
        if(update>0)
        {
            return true;
        }


        return false;
    }

    @Override
    public boolean updateFavoriteCount(int articleId, long favoriteCount) {
        int update= articlesDao.updateFavoriteCount(articleId, favoriteCount);
        if(update>0)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean userLiked(int articleId, int userId) {

        return articlesDao.userLiked(articleId, userId);
    }

    @Override
    public boolean userFavorited(int articleId, int userId) {
        return articlesDao.userFavorited(articleId, userId);
    }

    @Override
    public List<Favorite> findFavoriteList(int userId) {
        return articlesDao.findFavoriteList(userId);
    }

    @Override
    public List<Start> userLikedArticles(int userId, List<Integer> articleIds) {
        return articlesDao.userLikedArticles(userId, articleIds);
    }
}
